﻿using DatabaseTransfer.Connections;
using DatabaseTransfer.Enums;
using DatabaseTransfer.Logs;
using log4net;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DatabaseTransfer.Forms
{
    public partial class TransferSetup : Form
    {
        private static ILog logger = LogFactory.GetLogger(typeof(TransferSetup));
        public TransferSetup()
        {
            InitializeComponent();
#if DEBUG
            srcUrl.Text = "localhost";
            srcPort.Text = "3306";
            srcDbName.Text = "testdb";
            srcUserName.Text = "root";
            srcPassword.Text = "1qazXSW@";
            srcDbType.SelectedIndex = 1;

            destUrl.Text = "localhost";
            destPort.Text = "3306";
            destDbName.Text = "test";
            destUserName.Text = "root";
            destPassword.Text = "1qazXSW@";
            destDbType.SelectedIndex = 1;
#endif
        }

        private void continueBtn_Click(object sender, EventArgs e)
        {
            DatabaseConnector srcConnector = getSourceInfo();
            DatabaseConnector destConnecter = getDestinatioInfo();


            bool sourceValid = validateSource(srcConnector);
            bool destValid = false;
            if (sourceValid) { 
                destValid = validateDestination(destConnecter);
            }

            if (srcConnector == destConnecter)
            {
                MessageBox.Show("Source And Destination are the same. Unable to copy information. Please verify information");
                return;
            }

            if (sourceValid && destValid)
            {
                TableView view = new TableView(srcConnector, destConnecter);
                this.Close();
                view.Show();
            }

        }

        private bool validateDestination(DatabaseConnector destConnecter)
        {
            logger.Debug("Validating Destination");
            if (String.IsNullOrWhiteSpace(destConnecter.URL))
            {
                MessageBox.Show("Destination Url cannot be empty");
                return false;
            }

            if (String.IsNullOrWhiteSpace(destConnecter.Port))
            {
                MessageBox.Show("Destination Port cannot be empty");
                return false;
            }

            int port = 0;

            int.TryParse(destConnecter.Port, out port);

            if (port == 0)
            {
                MessageBox.Show("Destination Port Cannot Be Empty");
                return false;
            }

            if (String.IsNullOrWhiteSpace(destConnecter.Database))
            {
                MessageBox.Show("Destination Database Name cannot be empty");
                return false;
            }

            if (String.IsNullOrWhiteSpace(destConnecter.User))
            {
                MessageBox.Show("Destination User Name cannot be empty");
                return false;
            }

            if (String.IsNullOrWhiteSpace(destConnecter.Password))
            {
                MessageBox.Show("Destination Password cannot be empty");
                return false;
            }

            return true;
        }

        private bool validateSource(DatabaseConnector srcConnector)
        {
            logger.Debug("Validating Source");
            if (String.IsNullOrWhiteSpace(srcConnector.URL))
            {
                MessageBox.Show("Source Url cannot be empty");
                return false;
            }

            if (String.IsNullOrWhiteSpace(srcConnector.Port))
            {
                MessageBox.Show("Source Port cannot be empty");
                return false;
            }

            int port = 0;

            int.TryParse(srcConnector.Port, out port);

            if (port == 0)
            {
                MessageBox.Show("Source Port Cannot Be Empty");
                return false;
            }

            if (String.IsNullOrWhiteSpace(srcConnector.Database))
            {
                MessageBox.Show("Source Database Name cannot be empty");
                return false;
            }

            if (String.IsNullOrWhiteSpace(srcConnector.User))
            {
                MessageBox.Show("Source User Name cannot be empty");
                return false;
            }

            if (String.IsNullOrWhiteSpace(srcConnector.Password))
            {
                MessageBox.Show("Source Password cannot be empty");
                return false;
            }

            return true;
        }

        private DatabaseConnector getDestinatioInfo()
        {
            return new DatabaseConnector()
            {
                URL = destUrl.Text,
                Port = destPort.Text,
                Database = destDbName.Text,
                User = destUserName.Text,
                Password = destPassword.Text,
                DatabaseType = (DatabaseType) Enum.Parse(typeof(DatabaseType), destDbType.Text.ToUpper())
            };
        }

        private DatabaseConnector getSourceInfo()
        {
            return new DatabaseConnector()
            {
                URL = srcUrl.Text,
                Port = srcPort.Text,
                Database = srcDbName.Text,
                User = srcUserName.Text,
                Password = srcPassword.Text,
                DatabaseType = (DatabaseType)Enum.Parse(typeof(DatabaseType), srcDbType.Text.ToUpper())
            };
        }
    }
}
